package com.xiaobug.baselibrary.base;


import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.ljb.commlibrary.utils.AutoUtils;
import com.ljb.commlibrary.utils.L;

import me.yokeyword.fragmentation.SupportActivity;

/**
 * Activity基类
 * Created by ljb on 2018/4/26.
 */

public abstract class BaseActivity<T extends ViewDataBinding> extends SupportActivity {
    public final String TAG = this.getClass().getSimpleName();

    /**
     * 屏幕适配:该页面是否显示状态栏
     */
    protected boolean mHasStatusBar = true;
    /**
     * 屏幕适配:该页面设计宽
     */
    protected int mDesignWidth = 1080;
    /**
     * 屏幕适配:该页面设计高
     */
    protected int mDesignHeight = 1920;
    /**
     * DataBinding
     **/
    protected T mBinding;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //判断该Activity设置设置了布局文件
        if (getRootID() == 0) {
            L.e(TAG, "getRootID = 0,该Activity将不显示界面!!!");
            return;
        }

        //设置屏幕适配设计尺寸
        AutoUtils.setSize(this, mHasStatusBar, mDesignWidth, mDesignHeight);
        //初始化DataBinding
        mBinding = DataBindingUtil.setContentView(this, getRootID());
        //屏幕适配
        AutoUtils.auto(this);
        //初始化UI
        initUI(savedInstanceState, mBinding);
        //绑定数据
        initData(savedInstanceState, mBinding);
    }


    /**
     * 设置APP样式不会随系统样式改变,比如字体大小等
     *
     * @return
     */
    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        Configuration config = new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config, res.getDisplayMetrics());
        return res;
    }

    public void startActivity(Class<?> clz, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(this, clz);
        if (bundle != null)
            intent.putExtras(bundle);
        startActivity(intent);
    }

    public void startActivity(Class<?> clz) {
        startActivity(clz, null);
    }

    public void startActivityForResult(Class<?> clz, Bundle bundle, int requestCode) {
        Intent intent = new Intent();
        intent.setClass(this, clz);
        if (bundle != null)
            intent.putExtras(bundle);
        startActivityForResult(intent, requestCode);
    }

    public void startActivityForResult(Class<?> clz, int requestCode) {
        startActivityForResult(clz, null, requestCode);
    }

    //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@一下方法为子类实现的方法@@@@@@@@@@@@@@@@@@@@@@@@@@@

    /**
     * 获取根布局ID
     * 必须重写该方法,否则界面不会显示
     *
     * @return
     */
    protected abstract int getRootID();

    /**
     * 初始化控件
     *
     * @param savedInstanceState
     * @param binding
     */
    protected void initUI(Bundle savedInstanceState, T binding) {
    }

    /**
     * 绑定数据
     *
     * @param savedInstanceState
     * @param binding
     */
    protected void initData(Bundle savedInstanceState, T binding) {
    }


}
